package com.l.web.master.person;

import com.l.web.master.system.group.Group;
import com.l.web.master.system.group.GroupService;
import com.l.web.master.system.user.User;
import org.apache.log4j.Logger;
import org.l.qiao.Form;
import org.l.qiao.LConstant;
import org.l.qiao.annotation.Privilege;
import org.l.qiao.web.ControllerSupport;
import org.l.qiao.web.KeyUpdate;
import org.l.qiao.web.ResponseCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by Administrator on 2015/9/28.
 */
@Controller
@RequestMapping(PersonController.URI)
public class PersonController  extends ControllerSupport {
    public static final String URI = LConstant.MASTER_URI + "/person";
    private static final Logger LOGGER = Logger.getLogger(PersonController.class);
    @Autowired
    private PersonService personService;

    @RequestMapping("index.do")
    public ModelAndView index(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        return new ModelAndView(URI + "/index", modelMap);
    }

    @RequestMapping("update.do")
    public void update(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws IOException {
        Form form = getForm(request);
        KeyUpdate keyUpdate = form.getKeyUpdate(Person.class);
        LOGGER.debug("修改信息信息：" + keyUpdate);
        User user = getLoginUser(request);
        keyUpdate.setId(user.getId());

        if (keyUpdate.check()) {
            int code = ResponseCode.FAILURE;

            try {
                code = personService.update(keyUpdate);
            } catch (Exception e) {
                LOGGER.error("修改异常[" + keyUpdate + "]：" + e);
            }

            out(response, code);
        } else {
            LOGGER.error("非法修改：" + keyUpdate);
            response.sendError(404);
        }
    }
}
